package com.csw.mybatisSpringboot.config.quanXian;

import cn.hutool.core.bean.BeanUtil;
import com.csw.mybatisSpringboot.entity.User;
import io.jsonwebtoken.Claims;

import javax.servlet.http.HttpServletRequest;

public class UserUtils {

    /***
     * 获取当前登录用户uuid
     */
    public static User getUserInfo(HttpServletRequest request) throws Exception {
        return getUser(request);
    }


    /***
     * 获取当前登录用户uuid2
     */
    public static User getUserInfo() throws Exception {
        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        return getUser(request);
    }

    private static User getUser(HttpServletRequest request) throws Exception {
        String token = request.getHeader(TokenConstants.AUTHENTICATION).replaceAll(TokenConstants.PREFIX, "");
        Claims c = JwtUtil.parsejwt(token);
        Object o = c.get("user");
        User user = BeanUtil.copyProperties(o, User.class);
        return user;
    }
}
